package com.ebupt.migu.music.roster.controller;

import com.ebupt.migu.music.common.auth.AuthBaseUtils;
import com.ebupt.migu.music.common.auth.IDAuthUtils;
import com.ebupt.migu.music.common.entity.ResultObject;
import com.ebupt.migu.music.common.exception.MyException;
import com.ebupt.migu.music.common.exception.StatusEnum;
import com.ebupt.migu.music.common.util.ReqestParamUtil;
import com.ebupt.migu.music.roster.service.RosterService;
import com.ebupt.migu.music.roster.utils.ESutils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Created by Intellij IDEA.
 * User:  liuyangyang@cd.ebupt.com
 * Date:  2020/3/26
 */
@RestController
@RequestMapping("/roster")
public class RosterController {

    @Autowired
    private HttpServletRequest request;
    @Autowired
    private RosterService rosterService;
    @Autowired
    private IDAuthUtils idAuthUtils;
    @Autowired
    private ESutils eSutils;

    /**
     * 创建黑白名单
     *
     * @return
     */
    @RequestMapping("/add")
    public ResultObject addRoster() {
        // 获取用户信息和业务参数
        Map<String, Object> params = AuthBaseUtils.authBaseInfo(request);
        return rosterService.add(params);
    }

    /**
     * 追加黑白名单
     *
     * @return
     */
    @RequestMapping("/append")
    public ResultObject rosterAppend() {
        // 获取用户信息和业务参数
        Map<String, Object> params = AuthBaseUtils.authBaseInfo(request);
        return rosterService.append(params);
    }

    /**
     * 条件、分页查询名单列表
     * <p>
     * 1、用户手机号
     * 2、用户UID
     * 3、名单名称
     * 4、创建人
     * 5、创建人部门
     * 6、创建时间区间
     *
     * @return
     */
    @RequestMapping("/list")
    public ResultObject rosterList() {
        // 获取用户信息和业务参数
        Map<String, Object> params = ReqestParamUtil.getParamsWithUserInfo(request);
        idAuthUtils.isAdmin(params);
        return rosterService.rosterList(params);
    }

    /**
     * 查询名单明细
     *
     * @return
     */
    @RequestMapping("/details")
    public ResultObject rosterDetail() {
        Map<String, Object> params = AuthBaseUtils.authBaseInfo(request);
        return rosterService.rosterDetail(params);
    }

    /**
     * 删除名单分组
     *
     * @return
     */
    @RequestMapping("/delete")
    public ResultObject delete() {
        // 验证用户是否有此操作权限
        if (!idAuthUtils.isCreatorOrAdmin(request)) {
            throw new MyException(StatusEnum.OPERATE_LIMITED, "用户无权删除此分组");
        }
        // 获取用户信息和业务参数
        Map<String, Object> params = AuthBaseUtils.authBaseInfo(request);
        return rosterService.delete(params);
    }

    /**
     * 删除名单内指定成员
     *
     * @return
     */
    @RequestMapping("/delete_single")
    public ResultObject deleteSingle() {
        // 验证用户是否有此操作权限
        if (!idAuthUtils.isCreatorOrAdmin(request)) {
            throw new MyException(StatusEnum.OPERATE_LIMITED, "用户无权删除");
        }
        // 获取用户信息和业务参数
        Map<String, Object> params = AuthBaseUtils.authBaseInfo(request);
        return rosterService.deleteSingle(params);
    }

    /**
     * 测试接口- 反查用户信息
     */
    @RequestMapping("/inverse_search")
    public void inverse() {
        eSutils.inverse_search("8a2ad427-de65-49de-9c87-2261c5b9086f", "2");
    }
}